
$(function () {
    $("#jqGrid").Grid({
        url: '../exchangeCode/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id',align:"center", key: true, hidden: true},
            {label: '所属类别名称', name: 'giftTypeEntity.giftName', index: 'giftName',align:"center", width: 60},
            {label: '卡号', name: 'cardNumber', index: 'cardNumber', width: 60,align:"center",},
            {label: '兑换码', name: 'exchangeCode', index: 'exchangeCode', width: 100},
            {label: '礼包礼品', name: 'giftTypeEntity.giftContain', index: 'giftContain', width: 100},
            {
                label: '兑换状态', name: 'isUse', index: 'isUse', width: 80,align:"center", formatter: function (value) {
                    return value === 2 ?
                        '<span class="label label-danger">已兑换</span>' :
                        '<span class="label label-success">未兑换</span>';
                }
            }
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        gift: {
            giftTypeId:'',
            cardNumber:0,
            exchangeCode:''
        },
        chooseGift:{}, //选中的礼包类型全部信息
        giftTypeId:null, //选中的礼包类型
        giftTypes:[],  //所有礼包兑换类型
        adposition:{},
        ruleValidate: {
            giftTypeId: [
                {required: true, message: '名称不能为空', trigger: 'blur',type:'number'}
            ],
            cardNumber: [
                {required: true, message: '卡号不能为空', trigger: 'blur'}
            ],
            exchangeCode: [
                {required: true, message: '兑换码不能为空', trigger: 'blur'}
            ]

        },
        q: {
            param: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.gift = {width: 200, height: 750};
            vm.giftTypeId=null;
            vm.gift.giftContain=null;
            //获取到类型数据，并显示
            vm.getGiftType()
        },
        //批量导入
        importAll:function(){

        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id)
        },
        giftTypeChange:function(value){
            console.log(value,"选中的类型")
            var chooseGift=vm.giftTypes.filter(item=>{
                return item.id==value
            })
            vm.gift.giftContain=chooseGift[0].giftContain

            vm.gift.giftTypeId=value;
            vm.chooseGift=chooseGift[0];
        },
        saveOrUpdate: function (event) {
            var url = vm.gift.id == null ? "../exchangeCode/save" : "../exchangeCode/update";

            //更新的时候
            if(vm.gift.id != null){
                delete vm.gift.giftTypeEntity

            }
            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.gift),
                successCallback: function () {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });


        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    type: "POST",
                    url: "../exchangeCode/delete",
                    contentType: "application/json",
                    params: JSON.stringify(ids),
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            //获取礼包类型数据
            vm.getGiftType()
            Ajax.request({
                url: "../exchangeCode/info/" + id,
                async: true,
                successCallback: function (r) {

                    vm.gift = r.exchangeCodeEntity;
                    vm.giftTypeId=r.exchangeCodeEntity.giftTypeEntity.id
                    vm.gift.giftContain=r.exchangeCodeEntity.giftTypeEntity.giftContain
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'giftName': vm.q.param},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleActivityStartDate:function(time){
            vm.activityStartDate=time
        },
        handleActivityEndDate:function(time){
            vm.activityEndDate=time
        },
        //获取礼包类型数据
        getGiftType:function () {
            Ajax.request({
                url: "../giftType/list?limit=99999999&page=1&order=asc&sidx",
                type: "GET",
                contentType: "application/json",
                successCallback: function (r) {
                    console.log(r,'1111')
                    vm.giftTypes = r.page.list;
                }
            });
        },
        handleFormatError(file){
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传.xls,.xlsx文件。'
            })
        },
        handleSuccess(res,file){
            if(res.code === 0){
                this.dialoLead = false
                this.$Message.success("数据导入成功！")
                // this._getBookList()
                this.$refs.upload.clearFiles()
                vm.reload(); //重新加载页面
            }else{
                this.$Message.error(res.msg)

            }
        },
        handleError(error,file){
            this.$Message.error("数据导入失败！")
        },
        //下载模板
        downloadModel:function () {
            const a = document.createElement('a'); // 创建a标签
            a.setAttribute('download', '');// download属性
            a.setAttribute('href', '/platform-framework/exchangeCode/download');// href链接
            a.click();// 自执行点击事件
            a.remove(); //移除元素
        }
    }
});